package com.wujiale.javacore.internet;

import jakarta.enterprise.inject.New;

import javax.swing.*;
import java.awt.*;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * <p>InterruptibleSocketTest</p>
 *
 * @author WuJiaLe (15754904311@163.com)
 * @version v1.0.0
 */
public class InterruptibleSocketTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
                var frame = new InterruptibleSocketFrame();
                frame.setTitle("InterruptibleSocketTest");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            });
    }

    class InterruptibleSocketFrame extends JFrame
    {
        private Scanner in;
        private JButton interruptibleButton;
        private JButton blockingButton;
        private JButton cancelButton;
        private JTextArea messages;
        private TestServer testServer;
        private Thread connectThread;

        public InterruptibleSocketFrame(){
            var northPanel = new JPanel();
            add(northPanel, BorderLayout.NORTH);

            final int TEXT_ROWS = 20;
            final int TEXT_COLUMNS = 60;
            messages = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
            add(new JScrollPane(messages));

            interruptibleButton = new JButton("Interruptible");
            blockingButton = new JButton("Blocking");

            northPanel.add(interruptibleButton);
            northPanel.add(blockingButton);

            interruptibleButton.addActionListener(event -> {
                interruptibleButton.setEnabled(false);
                blockingButton.setEnabled(false);
                cancelButton.setEnabled(true);
                connectThread = new Thread(() -> {
                    try {
                        
                    }
                });
            });
        }
    }

    class TestServer implements Runnable
    {
        public void run(){
            try (var s = new ServerSocket(8189)){
                while (true){
                    Socket incoming = s.accept();
                    Runnable r = new TestServerHandler(incoming);
                    new Thread(r).start();
                }
            }catch (Exception e){
                messages.append("\nTestServer.run: "+e);
            }
        }
    }

    class TestServerHandler implements Runnable
    {
        private Socket incoming;
        private int counter;
        public TestServerHandler(Socket i){
            incoming = i;
        }

        public void run(){
            try {
                try {
                    OutputStream outputStream = incoming.getOutputStream();
                    var out = new PrintWriter(
                            new OutputStreamWriter(outputStream, StandardCharsets.UTF_8),
                            true);
                    while (counter < 100){
                        counter++;
                        if (counter <= 10) out.println(counter);
                        Thread.sleep(100);
                    }
                }finally {
                    incoming.close();
                    messages.append("closing server\n");
                }
            }catch (Exception e){
                messages.append("\nTestServerHandler.run: "+e);
            }
        }
    }
}
